package com.yuyu.qingmangmall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuyu.common.to.MemberPrice;
import com.yuyu.common.to.SkuReductionTo;
import com.yuyu.common.utils.PageUtils;
import com.yuyu.common.utils.Query;
import com.yuyu.qingmangmall.coupon.dao.SkuFullReductionDao;
import com.yuyu.qingmangmall.coupon.entity.MemberPriceEntity;
import com.yuyu.qingmangmall.coupon.entity.SkuFullReductionEntity;
import com.yuyu.qingmangmall.coupon.entity.SkuLadderEntity;
import com.yuyu.qingmangmall.coupon.service.MemberPriceService;
import com.yuyu.qingmangmall.coupon.service.SkuFullReductionService;
import com.yuyu.qingmangmall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    SkuLadderService skuLadderService;

    @Autowired
    MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void saveSkuReduction(SkuReductionTo skuReductionTo) {
        //      (ladder:范围, full:完整的, reduction:减少, member:会员, price:价格)
        // 5.4 sku的优惠、满减等信息  qingmangmall_sms -> sms_sku_ladder\sms_sku_full_reduction\sms_member_price

        // sms_sku_ladder
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        BeanUtils.copyProperties(skuReductionTo,skuLadderEntity);
        skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
        if (skuReductionTo.getFullCount() > 0) {
            skuLadderService.save(skuLadderEntity);
        }


        // sms_sku_full_reduction
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuReductionTo,skuFullReductionEntity);
        if (skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal("0"))==1) {
            this.save(skuFullReductionEntity);
        }

        // sms_member_price
        List<MemberPrice> memberPriceList = skuReductionTo.getMemberPrice();

        if (memberPriceList!=null && memberPriceList.size()>0){
            List<MemberPriceEntity> collect = memberPriceList.stream().map(member -> {
                MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
                memberPriceEntity.setMemberLevelId(member.getId());
                memberPriceEntity.setMemberPrice(member.getPrice());
                memberPriceEntity.setMemberLevelName(member.getName());
                memberPriceEntity.setAddOther(1);
                return memberPriceEntity;
            }).filter(item->{
                return item.getMemberPrice().compareTo(new BigDecimal("0")) ==1;
            }).collect(Collectors.toList());
            memberPriceService.saveBatch(collect);
        }



    }

}